﻿$(function(){

    //把userId参数加入到链接中
    var accountNode = $(".account");
    var accountHref = accountNode.attr("href");
    accountNode.attr("href",accountHref+"?id="+currentUser.id);

    $.getJsonData(contextPath + "/device/list",{},{type:"Get"}).done(function(data){
        if(data.success){
            var result = data.result.items;//取数据列表
            var deviceNode = $(".template .devices");//获得device的模板
            $.each(result,function(){
                var cloneNode = deviceNode.clone(true);
                cloneNode.find(".deviceId span").html(this.deviceId);
                cloneNode.find(".title").html(this.title);
                cloneNode.attr("data-deviceId",this.deviceId);
                cloneNode.find(".tags span").html(this.tags);
                cloneNode.find(".about span").html(this.about);
                //把deviceId参数加入到链接中
                var manageNode = cloneNode.find(".button.manage");
                var manageHref = manageNode.attr("href");
                manageNode.attr("href",manageHref+"?deviceId="+this.deviceId)
                $("#deviceList").append(cloneNode);
            })
        }
    }).fail(function(e){
        console.log(e);
    })

    $("#loginOut").on("click",function(){
        $.getJsonData(fullServerPath+"/users/logout",{},{type:"Post"}).done(function(data){
            if(data.success){
                moon.info("退出","top");
                window.location.href = contextPath+"login.html";
            }else{
                moon.error("退出失败","top");
            }
        })
    })

    $("#deviceList").on("click",".delete", function (e) {
        var deviceNode = $(e.currentTarget).closest(".devices");
        var deviceId = deviceNode.attr("data-deviceId");
        console.log(deviceId);
        $.getJsonData(contextPath + "/device/"+deviceId, {}, {type: "Delete"}).done(function (data) {
            console.log(data);
            if(data.success){
                window.location.href=contextPath+"dev_suc_del.html";
                deviceNode.remove();
            }else{
                moon.error("删除失败","top");
            }
        })
    })
})

